package com.xx.springboot.test.RabbitMQ.work;

/**
 * decr:
 * create by Xiaoxi
 * email:xiaoxiunique@qq.com
 * phone:13541863172
 * date:2018/8/30/030 10:15
 * project_name:SpringBoot-MybatisPlus
 * package_name:com.xx.springboot.test.RabbitMQ.simple
 */

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import com.xx.springboot.test.RabbitMQ.RabbitMQUtil;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;

/**
 * 定义 消息队列的 生产者
 */
public class Producter {
    private static final String QUEUE_NAME = "durable_queque1";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 从连接工厂中 得到一个连接
        Connection connection = RabbitMQUtil.getConnection();

        // 通过连接 创建一个通道
        Channel channel = connection.createChannel();

        // 是否保证队列持久化
        boolean durable = true;

        // 创建队列申明
        channel.queueDeclare(QUEUE_NAME, durable, false, false, null);

        /**
         * 定义发送消息的内容
         */
        StringBuffer stringBuffer = null;

        long startTime = System.currentTimeMillis();

        /**
         * 创建线程池 进行消息发送测试
         */
        ExecutorService threadPool = Executors.newCachedThreadPool();

        for (int j = 0; j < 1000; j++) {
            try{
                stringBuffer = new StringBuffer("hello work queue");
                stringBuffer.append(j);
                stringBuffer.append(".");
                channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, stringBuffer.toString().getBytes());
                System.out.println(stringBuffer.toString() + "send successful !");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        long endTime = System.currentTimeMillis();


        System.out.println("总的消耗时间" +  (endTime - startTime));
        /**
         * 操作完成之后  关闭通道  关闭连接
         */

        //channel.close();
        //connection.close();
    }
}
